package com.oocl.chat.controller;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.oocl.chat.util.Constants;
import com.oocl.thread.SocketListener;


public class ChatServer extends Thread{	
	private static ServerSocket server;
	public static Socket socket;
	public static ChatServer chatServer = new ChatServer();
	private boolean isExit = false;
	
	@Override
	public void run() {
		open();
	}
	
	public void open() {
		isExit = false;
		try {
			server = new ServerSocket(Constants.PORT1);
			System.out.println("服务器开启成功!!!");
			while(!isExit){
				socket = server.accept();
				new Thread(new SocketListener(socket)).start();
			}
		} catch (Exception e) {
			isExit = true;
		}
	}
	public void shutdown(){
		isExit = true;
		try {
			if(server!=null){
				socket.close();
				server.close();
				System.out.println("服务器关闭");
			}
			isExit = true;
//			server.close();
		} catch (IOException e) {
			isExit = true;
			System.out.println("关闭");
		}
	}
	
	public static void main(String[] args) throws Exception {
		new ChatServer().start();
	}
}
